define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'superdata/activitytags/index' + location.search,
                    add_url: 'superdata/activitytags/add',
                    edit_url: 'superdata/activitytags/edit',
                    // del_url: 'superdata/activitytags/del',
                    // multi_url: 'superdata/activitytags/multi',
                    // import_url: 'superdata/activitytags/import',
                    table: 'activity_tags',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                //sortName: 'id',
                sortName: 'sort desc,id desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'admin_id', title: __('Admin_id')},
                        // {field: 'status', title: __('Status')},


                        {field: 'sort', title: __('Sort'), operate:false},
                        {field: 'createTime', title: __('CreateTime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {
                        //     field:'status_text',
                        //     title: __('Status'),
                        //     operate:false,
                        //     formatter:Table.api.formatter.status,
                        // },
                        {field: 'status_text', title: __('Status'), operate: false,formatter:function(value,row){
                                if (row['status'] == 1)
                                {
                                    return '<span style="color:yellowgreen">'+value+'</span>';
                                }else {
                                    return '<span style="color:black">'+value+'</span>';
                                }
                            }
                        },
                        {
                            field: 'status',
                            title: __('Status') ,
                            visible:false,
                            formatter: Table.api.formatter.normal,
                            searchList: { '0': '无效','1': '有效'},
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),

                            buttons: [
                                {
                                    name: 'ajax',
                                    text: function(row){
                                        if(row['status']===1) {
                                            return '停用';
                                        }
                                        else{
                                            // return '<span style="color:yellowgreen">上架</span>';
                                            return '启用';
                                        }

                                    },
                                    title: __('发送请求'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'superdata/activitytags/detail?ids={ids}&status={status}',
                                    confirm: '确认发送操作请求？',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        table.bootstrapTable('refresh',{});
                                        return false;

                                    },
                                    error: function (data, ret) {
                                        //console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                             ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});